﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using LPrisaMVC.Entities;
using LPrisaMVC.BusinessLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;



namespace LPrisaMVC.WebMVC4.Controllers
{
    public class CategoriaController : Controller
    {
        public CategoriaController()
        {

            //using (IUnityContainer container = new UnityContainer())
            //{
            //    container.LoadConfiguration();
            //    container.BuildUp(this.GetType(), this);
            //}
        }

        [Dependency]
        public ICategoriaBL catBL{ get; set; }


        [Authorize(Roles = "admin, ventas")]
        public ActionResult Index()
        {
            var categorias = catBL.GetFromCategoriaByDescripcion("");
            return View(categorias);
        }


        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Index(string criterio = "")
        {
            var categorias = catBL.GetFromCategoriaByDescripcion(criterio);
            return View(categorias);
        }

        [Authorize(Roles = "admin, ventas")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //GET por default
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Create(CATEGORIA categoria)
        {
            try
            {
                catBL.AddCategoria(categoria);
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Categoria/Edit/
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Edit(int id = 0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Categoria");
            }

            var categorias = catBL.GetFromCategoriaById(id);
            if (categorias == null)
            {
                return RedirectToAction("Index", "Categoria");
            }

            return View(categorias);
        }

        //
        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Edit(int id, CATEGORIA categoria)
        {
            try
            {
                catBL.UpdateCategoria(categoria);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
        //[HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Delete(int id)
        {
            try
            {
                catBL.RemoveCategoria(id);

                return RedirectToAction("Index");  
                //return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return RedirectToAction("Index");  
                //return Json(false, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
